package com.telchina.data.meterReading.service.impl;

import com.alibaba.fastjson2.JSONObject;
import com.telchina.common.utils.uuid.Seq;
import com.telchina.data.meterReading.entity.MeterReading;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.telchina.data.meterReading.mapper.MeterreadingMapper;
import com.telchina.data.meterReading.service.IMeterreadingService;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * 水表读数(Meterreading)表服务实现类
 *
 * @author 郝振
 * @since 2023-06-16 17:38:43
 */
@Service("meterreadingService")
public class MeterreadingServiceImpl extends ServiceImpl<MeterreadingMapper, MeterReading> implements IMeterreadingService {

    @Autowired
    private MeterreadingMapper meterreadingmapper;


    /**
     * @param message
     * @return
     */
    @Transactional
    public void saveMeterReading(String message) throws ParseException {
        JSONObject jsonObject = JSONObject.parseObject(message);
        JSONObject services = jsonObject.getJSONObject("services");
        JSONObject waterMeter = services.getJSONObject("水表").getJSONObject("properties");
        String deviceId = jsonObject.getString("d_id");
        String ts = jsonObject.getString("ts");
        String meterNo = waterMeter.getJSONObject("表号").getString("value").replace("[", "").replace("]", "");
        String scale = waterMeter.getJSONObject("读数").getString("value").replace("[", "").replace("]", "");
        String[] meterNoArr = meterNo.split(",");
        String[] scaleArr = scale.split(",");
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
        Date readTime = format.parse(ts);
        List<MeterReading> meterReadingctList = new ArrayList<>();
        for (int i = 0; i < meterNoArr.length; i++) {
            MeterReading meterreading = new MeterReading();
            meterreading.setConcentratorNo(deviceId);
            meterreading.setMeterNo(meterNoArr[i]);
            meterreading.setScale(Double.valueOf(scaleArr[i]));
            meterreading.setCreateTime(new Date());
            meterreading.setCreateName("iotedge");
            meterreading.setReadTime(readTime);
            meterreading.setStatus("4F");
            save(meterreading);
            //meterReadingctList.add(meterreading);
        }
        //saveBatch(meterReadingctList);

    }


}
